#include "ScoreBoard.h"
#include "DataManage.h"
#include "AchievementManage.h"
#include "StartLayer.h"
#include "AudioManage.h"

static const char* COM1_ICON_IMAGE = "PlayerBoard/icon1.png";
static const char* COM2_ICON_IMAGE = "PlayerBoard/icon2.png";
static const char* COM3_ICON_IMAGE = "PlayerBoard/icon3.png";
static const char* COM4_ICON_IMAGE = "PlayerBoard/icon4.png";
static const char* BUTTON_CLOSE = "SettingBoard/help_exit.png";

bool ScoreTable::init()
{
	m_pTableView = TableView::create(this, Size(455, 640));
	m_pTableView->setDirection(cocos2d::extension::ScrollView::Direction::VERTICAL);
	m_pTableView->setPosition(Point(10, 10));
	m_pTableView->reloadData();

	addChild(m_pTableView);

	return true;
}

Size ScoreTable::cellSizeForTable(TableView* pTable)
{
	return Size(445, 125);
}

TableViewCell* ScoreTable::tableCellAtIndex(TableView* pTable, ssize_t idx)
{
	TableViewCell* cell = ScoreCell::create();
	ScoreCell* pCell = dynamic_cast<ScoreCell*>(cell);
	pCell->updateContent(idx);

	return cell;
}

ssize_t ScoreTable::numberOfCellsInTableView(TableView* pTable)
{
	return UserDefault::getInstance()->getIntegerForKey(USER_DEFAULT_RESULT_COUNT, 0);
}

bool ScoreCell::init()
{
	if (!Node::init())
	{
		return false;
	}

	m_pCellBase = Sprite::create("ScoreBoard/scoreCell.png");
	m_pCellBase->setAnchorPoint(Vec2::ZERO);
	m_pCellBase->setPosition(Point(0, 0));
	addChild(m_pCellBase);

	m_pIconSprite = Sprite::create();
	m_pIconSprite->setPosition(m_pCellBase->getContentSize().width * 0.2f, m_pCellBase->getContentSize().height * 0.5f);
	m_pIconSprite->setScale(0.8f);
	m_pCellBase->addChild(m_pIconSprite);

	m_pDataLabel = Label::createWithTTF("", "fonts/arial.ttf", 32);
	m_pDataLabel->setAnchorPoint(Point::ANCHOR_MIDDLE_LEFT);
	m_pDataLabel->setPosition(m_pCellBase->getContentSize().width * 0.35f, m_pCellBase->getContentSize().height * 0.7f);
	m_pDataLabel->enableOutline(Color4B::GRAY, 2);
	m_pDataLabel->setTextColor(Color4B::WHITE);
	m_pCellBase->addChild(m_pDataLabel);

	m_pRankLabel = Label::createWithTTF("", "fonts/arial.ttf", 38);
	m_pRankLabel->setAnchorPoint(Point::ANCHOR_MIDDLE_LEFT);
	m_pRankLabel->setPosition(m_pCellBase->getContentSize().width * 0.35f, m_pCellBase->getContentSize().height * 0.35f);
	m_pRankLabel->enableOutline(Color4B::WHITE, 2);
	m_pRankLabel->setTextColor(Color4B::RED);
	m_pCellBase->addChild(m_pRankLabel);

	m_pMoneyLabel = Label::createWithTTF("", "fonts/arial.ttf", 32);
	m_pMoneyLabel->setAnchorPoint(Point::ANCHOR_MIDDLE_LEFT);
	m_pMoneyLabel->setPosition(m_pCellBase->getContentSize().width * 0.55f, m_pCellBase->getContentSize().height * 0.35f);
	m_pMoneyLabel->enableOutline(Color4B::WHITE, 2);
	m_pMoneyLabel->setTextColor(Color4B::ORANGE);
	m_pMoneyLabel->enableItalics();
	m_pCellBase->addChild(m_pMoneyLabel);

	return true;
}

void ScoreCell::updateContent(int cellID)
{
	float flWidth = m_pCellBase->getContentSize().width;
	float flHeight = m_pCellBase->getContentSize().height;

	int nIcon = UserDefault::getInstance()->getIntegerForKey(__String::createWithFormat(USER_DEFAULT_RESULT_ICON, cellID)->getCString(), 0);
	m_pIconSprite->setTexture(__String::createWithFormat("SettingBoard/PlayerIcon%d.png", nIcon + 1)->getCString());

	string strData = UserDefault::getInstance()->getStringForKey(__String::createWithFormat(USER_DEFAULT_RESULT_DATA, cellID)->getCString(), "2017-6-27");
	m_pDataLabel->setString(strData);

	int nRank = UserDefault::getInstance()->getIntegerForKey(__String::createWithFormat(USER_DEFAULT_RESULT_RANK, cellID)->getCString(), 0);
	int nPlayerCount = UserDefault::getInstance()->getIntegerForKey(__String::createWithFormat(USER_DEFAULT_RESULT_PLAYER_COUNT, cellID)->getCString(), 0);
	string strRank = __String::createWithFormat("%d/%d", nRank, nPlayerCount)->getCString();
	m_pRankLabel->setString(strRank);

	int nMoney = UserDefault::getInstance()->getIntegerForKey(__String::createWithFormat(USER_DEFAULT_RESULT_MONEY, cellID)->getCString(), 0);
	string strMoney = __String::createWithFormat("$%d,000", nMoney)->getCString();
	m_pMoneyLabel->setString(strMoney);
}

ScoreBoard* ScoreBoard::create(int nType)
{
	ScoreBoard *pRet = new(std::nothrow) ScoreBoard();
	if (pRet && pRet->init(nType))
	{
		pRet->autorelease();
		return pRet;
	}
	else
	{
		delete pRet;
		pRet = nullptr;
		return nullptr;
	}
}

bool ScoreBoard::init(int nType)
{
	if (!Layer::init())
	{
		return false;
	}

	if (nType == GAME_SCORE)
	{
		return gameScoreInit();
	}
	else
	{
		return historyScoreInit();
	}
}

bool ScoreBoard::gameScoreInit()
{
	Size viewSize = Director::getInstance()->getVisibleSize();

	LayerColor* pBaseColor = LayerColor::create(Color4B(0, 0, 0, 150), viewSize.width * 2, viewSize.height * 2);
	pBaseColor->setPosition(viewSize.width / 2 * -1, viewSize.height / 2 * -1);
	addChild(pBaseColor);

	Sprite* pBoardBaseSprite = Sprite::create("ScoreBoard/scoreBase.png");
	pBoardBaseSprite->setPosition(viewSize.width / 2, viewSize.height * 0.65f);
	addChild(pBoardBaseSprite);

	Sprite* pTitleSprite = Sprite::create("ScoreBoard/fontScores.png");
	pTitleSprite->setPosition(pBoardBaseSprite->getContentSize().width / 2, pBoardBaseSprite->getContentSize().height * 0.93f);
	pBoardBaseSprite->addChild(pTitleSprite);

	Sprite* pInnerBoard = Sprite::create("ScoreBoard/scoreInnerBase.png");
	pInnerBoard->setPosition(pBoardBaseSprite->getContentSize().width / 2, pBoardBaseSprite->getContentSize().height * 0.44f);
	pBoardBaseSprite->addChild(pInnerBoard);

	if (initCurrentGameTable(pInnerBoard->getContentSize()))
	{
		pInnerBoard->addChild(m_pCurrentGameTobale);
	}

	MenuItemImage* pExitBtn = MenuItemImage::create("ForSaleBoard/button_exit.png", "ForSaleBoard/button_exit.png", CC_CALLBACK_0(ScoreBoard::onExitButtonClick, this));
	pExitBtn->setPosition(pBoardBaseSprite->getContentSize().width * 0.85f, pBoardBaseSprite->getContentSize().height * 0.9f);
	pExitBtn->setScale(1.3f);

	Menu* pButtonMenut = Menu::create(pExitBtn, nullptr);
	pButtonMenut->setPosition(Point::ZERO);
	pBoardBaseSprite->addChild(pButtonMenut);

	setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
	setTouchEnabled(true);
	setSwallowsTouches(true);
	 
	return true;
}

bool ScoreBoard::historyScoreInit()
{
	Size viewSize = Director::getInstance()->getVisibleSize();

	LayerColor* pBaseColor = LayerColor::create(Color4B(0, 0, 0, 150), viewSize.width * 2, viewSize.height * 2);
	pBaseColor->setPosition(viewSize.width / 2 * -1, viewSize.height / 2 * -1);
	addChild(pBaseColor);

	Sprite* pBoardBaseSprite = Sprite::create("ScoreBoard/scoreBase.png");
	pBoardBaseSprite->setPosition(viewSize.width / 2, viewSize.height * 0.65f);
	addChild(pBoardBaseSprite);

	Sprite* pTitleSprite = Sprite::create("ScoreBoard/fontScores.png");
	pTitleSprite->setPosition(pBoardBaseSprite->getContentSize().width / 2, pBoardBaseSprite->getContentSize().height * 0.93f);
	pBoardBaseSprite->addChild(pTitleSprite);

	Sprite* pInnerBoard = Sprite::create("ScoreBoard/scoreInnerBase.png");
	pInnerBoard->setPosition(pBoardBaseSprite->getContentSize().width / 2, pBoardBaseSprite->getContentSize().height * 0.44f);
	pBoardBaseSprite->addChild(pInnerBoard);

	MenuItem* pCloseButton = MenuItemImage::create(BUTTON_CLOSE, BUTTON_CLOSE, CC_CALLBACK_0(ScoreBoard::onCloseButtonClick, this));
	pCloseButton->setPosition(pBoardBaseSprite->getContentSize().width * 0.90f, pBoardBaseSprite->getContentSize().height * 0.93f);
	pCloseButton->setScale(1.5f);

	Menu* pBtnMenu = Menu::create(pCloseButton, nullptr);
	pBtnMenu->setPosition(Point::ZERO);
	pBoardBaseSprite->addChild(pBtnMenu);

	ScoreTable* pTable = ScoreTable::create();
	pTable->setPosition(-5, 0);
	pInnerBoard->addChild(pTable);

	setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
	setTouchEnabled(true);
	setSwallowsTouches(true);

	return true;
}

bool ScoreBoard::initCurrentGameTable(Size innerSize)
{
	m_pCurrentGameTobale = Node::create();
	m_pCurrentGameTobale->setCascadeOpacityEnabled(true);
	m_pCurrentGameTobale->setContentSize(innerSize);

	vector<int> rgRank = DATA_MANAGE->getFinalRank();

	string rgIcons[4] = { COM1_ICON_IMAGE, COM2_ICON_IMAGE, COM3_ICON_IMAGE, COM4_ICON_IMAGE };

	for (int i = 0; i < 5; i++)
	{
		Sprite* pCell = Sprite::create("ScoreBoard/scoreCell.png");
		pCell->setPosition(innerSize.width / 2, innerSize.height - (18 + pCell->getContentSize().height / 2) - (pCell->getContentSize().height + 5) * i );
		m_pCurrentGameTobale->addChild(pCell);

		if (i < (int)rgRank.size())
		{
			int nPlayerIndex = rgRank.at(i);

			if (nPlayerIndex >= 0)
			{
				Sprite* pIconSprite = Sprite::create(rgIcons[nPlayerIndex]);
				pIconSprite->setScale(0.9f);
				pIconSprite->setPosition(pCell->getContentSize().width * 0.3f, pCell->getContentSize().height * 0.5f);
				pCell->addChild(pIconSprite);
			}
			else
			{
				int nIcon = AchievementManage::getInstance()->getCurrentIconIndex();
				Sprite* pIconSprite = Sprite::create(__String::createWithFormat("SettingBoard/PlayerIcon%d.png", nIcon + 1)->getCString());
				pIconSprite->setScale(0.9f);
				pIconSprite->setPosition(pCell->getContentSize().width * 0.3f, pCell->getContentSize().height * 0.5f);
				pCell->addChild(pIconSprite);
			}

			char rank[10] = {""};
			sprintf(rank, "%d", i + 1);

			Label* pRankLabel = Label::createWithTTF(rank, "fonts/arial.ttf", 48);
			pRankLabel->setTextColor(Color4B::WHITE);
			pRankLabel->enableOutline(Color4B(242, 214, 132, 255), 1.5f);
			pRankLabel->setPosition(pCell->getContentSize().width * 0.1f, pCell->getContentSize().height * 0.5f);
			pCell->addChild(pRankLabel);

			char a[30] = { "$0" };
			sprintf(a, "$%d,000", DATA_MANAGE->getMoneyByPlayerIndex(nPlayerIndex));

			Label* pMoneyLable = Label::createWithTTF(a, "fonts/arial.ttf", 48);
			pMoneyLable->setTextColor(Color4B::WHITE);
			pMoneyLable->enableOutline(Color4B(242, 214, 132, 255), 1.5f);
			pMoneyLable->setPosition(pCell->getContentSize().width * 0.68f, pCell->getContentSize().height * 0.5f);
			pCell->addChild(pMoneyLable);
		}
	}

	return true;
}

bool ScoreBoard::onTouchBegan(Touch *touch, Event *event)
{
	return true;
}

void ScoreBoard::onCloseButtonClick()
{
	AudioManage::button2Sound();
	removeFromParent();
}

void ScoreBoard::onExitButtonClick()
{
	AudioManage::button2Sound();
	Scene* pStartScene = StartLayer::createScene();
	Director::getInstance()->replaceScene(pStartScene);
}